package org.client.iot.ui.device;

import android.annotation.SuppressLint;

import com.lucky.base.network.NetworkApi;
import com.lucky.base.network.observer.BaseObserver;
import com.lucky.utils.FastjsonUtils;
import com.lucky.utils.log.LogUtils;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;

import org.client.iot.R;
import org.client.iot.databinding.ActivityEquipmentInspectionBinding;
import org.client.iot.domain.SensorDetectionDto;
import org.client.iot.http.ApiService;
import org.client.iot.http.BaseDevReq;
import org.client.iot.util.TimeUtils;
import org.tzw.template.base.BaseActivity;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 设备检验
 */
public class EquipmentInspectionActivity extends BaseActivity<ActivityEquipmentInspectionBinding> {
    private MiniLoadingDialog mMiniLoadingDialog;

    @Override
    protected void initView() {
        setSupportActionBar(mRoot.toolbarEquipmentInspection.toolbarPageHead);
        mRoot.toolbarEquipmentInspection.textViewTitle.setText(getString(R.string.production_equipment_inspection_title));
    }

    @Override
    protected void initListener() {
        mRoot.toolbarEquipmentInspection.imgBack.setOnClickListener(v -> {
            finish();
        });
        mRoot.btnEquipmentInspectionBtnTest.setOnClickListener(v -> {
            mMiniLoadingDialog = WidgetUtils.getMiniLoadingDialog(this, "设备传感器检测中...");
            mMiniLoadingDialog.show();
            devSensorDetection();
        });
    }

    // 传感器检测接口
    private void devSensorDetection() {
        String params = FastjsonUtils.toJsonString(devSensorDetectionParams());
        Observable<BaseDevReq<SensorDetectionDto>> compose =
                NetworkApi.createService(ApiService.class, "dev").devSensorDetection
                                (RequestBody.create(MediaType.parse("application/json"), params))
                        .compose(NetworkApi.applySchedulers(new BaseObserver<BaseDevReq<SensorDetectionDto>>() {
                            @SuppressLint("CheckResult")
                            @Override
                            public void onSuccess(BaseDevReq<SensorDetectionDto> req) {
                                if (req == null) {
                                    return;
                                }
                                runOnUiThread(() -> {
                                    disLoading();
                                    LogUtils.i("传感器检测接口req==", req.toString());
                                    showToastShort(req.getMessage());
                                    if (req.getData() != null) {
                                        mRoot.txtElectricalMachineryStatus.setText(req.getData().getElectricalMachineryStatus());
                                        mRoot.txtSensorCommunicationStatus.setText(req.getData().getSensorCommunicationStatus());
                                        mRoot.txtSensorNetCommunicationStatus.setText(req.getData().getSensorNetCommunicationStatus());
                                        mRoot.txtWaterSensorCommunicationStatus.setText(req.getData().getWaterSensorCommunicationStatus());
                                        mRoot.txtWirelessChargingStatus.setText(req.getData().getWirelessChargingStatus());
                                    }


                                });

                            }

                            @SuppressLint("CheckResult")
                            @Override
                            public void onFailure(Throwable e) {
                                runOnUiThread(() -> {
                                            disLoading();
                                            showToastLong(e.getMessage());
                                        }
                                );

                            }
                        }));
    }

    private Map<String, Object> devSensorDetectionParams() {
        Map<String, Object> params = new LinkedHashMap<>();
        params.put("start_time", TimeUtils.getYearAndMonthAndDayTime(new Date()));
        return params;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        disLoading();
    }

    private void disLoading() {
        runOnUiThread(() -> {
            if (mMiniLoadingDialog != null) {
                mMiniLoadingDialog.dismiss();
            }
        });

    }
}